package com.hello.web.service.user;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hello.common.utils.PageResponse;
import com.hello.common.utils.Response;
import com.hello.web.domain.bo.user.AddUserBO;
import com.hello.web.domain.bo.user.DelUserBO;
import com.hello.web.domain.bo.user.UserQueryBO;
import com.hello.web.domain.entity.RoleDO;
import com.hello.web.domain.entity.UserDO;
import com.hello.web.domain.entity.UserRoleDO;
import com.hello.web.domain.mapper.RoleMapper;
import com.hello.web.domain.mapper.UserMapper;
import com.hello.web.domain.mapper.UserRoleMapper;
import com.hello.web.domain.vo.user.UserVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @date 2025/2/24 15:31
 */
@Service
@RequiredArgsConstructor
public class UserService {

    private final UserMapper userMapper;
    private final RoleMapper roleMapper;
    private final UserRoleMapper userRoleMapper;


    public PageResponse queryUserPageList(UserQueryBO bo) {
        Page<UserDO> page = new Page<>(bo.getCurrent(), bo.getSize());
        LambdaQueryWrapper<UserDO> wrapper = new LambdaQueryWrapper<>(UserDO.class)
                .like(StrUtil.isNotBlank(bo.getName()), UserDO::getUsername, bo.getName())
                .between(bo.getStartDate() != null && bo.getEndDate() != null,
                        UserDO::getCreateTime, bo.getStartDate(), bo.getEndDate());
        Page<UserDO> userDOPage = userMapper.selectPage(page, wrapper);
        List<UserDO> userDOList = userDOPage.getRecords();

        List<UserVO> userVOS = userDOList.stream()
                .map(userDO -> {
                    UserRoleDO userRole = userRoleMapper.findRoleIdByUserId(userDO.getId());
                    RoleDO roleDO = roleMapper.selectById(userRole.getRoleId());

                    return UserVO.builder()
                            .id(userDO.getId())
                            .username(userDO.getUsername())
                            .nickname(userDO.getNickname())
                            .phone(userDO.getPhone())
                            .status(userDO.getStatus())
                            .createTime(userDO.getCreateTime())
                            .roleId(roleDO.getId())
                            .roleName(roleDO.getName())
                            .build();
                }).collect(Collectors.toList());
        return PageResponse.success(userDOPage, userVOS);
    }


    public Response queryUserList() {
        return Response.success(userMapper.selectList(null));
    }

    public UserDO addUser(AddUserBO vo) {
        if (vo.getId() == null) {
            userMapper.insert(UserDO.builder()
                    .username(vo.getUsername())
                    .password(SecureUtil.md5(vo.getPassword()))
                    .nickname(vo.getNickname())
                    .phone(vo.getPhone())
                    .status(1)
                    .build());
            userRoleMapper.insert(UserRoleDO.builder()
                    .userId(userMapper.findByUsername(vo.getUsername()).getId())
                    .roleId(vo.getRoleId())
                    .build());
        }else {
            UserDO userDO = userMapper.findByUsername(vo.getUsername());
            BeanUtil.copyProperties(vo, userDO, CopyOptions.create().setIgnoreNullValue(true));
            userMapper.updateById(userDO);
        }
        return userMapper.findByUsername(vo.getUsername());
    }

    public void deleteUser(DelUserBO bo) {
        userMapper.deleteById(bo.getId());
        userRoleMapper.delete(new LambdaQueryWrapper<UserRoleDO>().eq(UserRoleDO::getUserId, bo.getId()));
    }


}
